package com.huilian.iotbox.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.UserDto;
import com.huilian.iotbox.data.enums.OperationLogEnum;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.enums.UserStatusEnum;
import com.huilian.iotbox.data.po.User;
import com.huilian.iotbox.data.utils.Util;
import com.huilian.iotbox.data.vo.UserInfo;
import com.huilian.iotbox.data.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 少凡
 * @date 2020/2/26 15:58
 */
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 获取登用的用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("/userInfo")
    public ServerResponse getUserInfo(HttpServletRequest request) {

        UserInfo userInfo = userService.getLoginUserInfo(request);

        return ServerResponse.createBySuccess(userInfo);
    }

    /**
     * 查询用户并且查询角色  保留后期可以自己增加搜索按钮
     *
     * @param userVo  查询用户并且查询角色
     * @param request
     * @return
     */
    @PostMapping("/findStaffListByAgencyId")
    public ServerResponse findStaffListByAgencyId(@RequestBody UserVo userVo,
                                                  HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        PageHelper.startPage(userVo.getPageNum(), userVo.getPageSize());
        userVo.setUserInfo(userInfo);
        List<UserVo> list = userService.findStaffListByAgencyId(userVo);
        PageInfo pageResult = new PageInfo(list);
        return ServerResponse.createBySuccess(pageResult);
    }

    /**
     * 验证登录账号是否唯一
     *
     * @param user
     * @return
     */
    @GetMapping("/checkUserName")
    public ServerResponse checkUserName(User user) {
        return userService.checkUserName(user);
    }

    /**
     * 查看身份证是否唯一
     *
     * @param idCardNo
     * @param agencyId
     * @return
     */
    @GetMapping("/checkIdCardNo")
    public ServerResponse checkIdCardNo(String idCardNo, String agencyId, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return userService.checkIdCardNo(idCardNo, agencyId, userInfo);
    }

    /**
     * 查看手机号是否唯一
     *
     * @param mobile
     * @return
     */
    @GetMapping("/checkMobile")
    public ServerResponse checkMobile(String mobile, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return userService.checkMobile(mobile);
    }

    /**
     * 验证手机号码和登录账户是否唯一   登录时可以用手机号码或登录账户登录所以要确保两个都唯一
     **/
    @GetMapping("/checkMobileAndUsername")
    public ServerResponse checkMobileAndUsername(String mobile, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        // 验证手机号码
        ServerResponse response = userService.checkMobile(mobile);
        if (OperationLogEnum.STATUS_SUCC.getCode().equals(Util.stringConvert(response.getStatus()))) {
            // 验证登录账号
            User user = new User();
            user.setUsername(mobile);
            response = userService.checkUserName(user);
        }
        return response;
    }

    /**
     * 根据关键字查询员工,带分页
     *
     * @param request
     * @param userDto
     * @return
     */
    @PostMapping("/findByNameAndMobile")
    public ServerResponse findByNameAndMobile(HttpServletRequest request, @RequestBody UserDto userDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(userService.findByNameAndMobile(userDto));
    }

    @GetMapping("/getAllUserOfName")
    public ServerResponse getAllUserOfName(HttpServletRequest request, String agencyId) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(userService.getAllUserOfName(userInfo, agencyId));
    }
    @GetMapping("/getAllUserSubAccountOfName")
    public ServerResponse getAllUserSubAccountOfName(HttpServletRequest request, String userIdType) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        return ServerResponse.createBySuccess(userService.getAllUserSubAccountOfName(userInfo,userIdType));
    }


    @PostMapping("/list")
    public ServerResponse findUserList(HttpServletRequest request, @RequestBody UserDto userDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        userDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        userDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return ServerResponse.createBySuccess(userService.findUserList(userDto));
    }

    // 商户注册时获取短信验证
    @GetMapping("/registerSmsCode/{mobile}")
    public ServerResponse registerSmsCode(@PathVariable String mobile, HttpServletRequest request) {
        return userService.registerSmsCode(mobile);
    }

    /**
     * 注册商户
     **/
    @PostMapping("/register")
    public ServerResponse register(@RequestBody UserDto userDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.register(userDto);
    }

    @PutMapping("/updatePwd")
    public ServerResponse updatePwd(HttpServletRequest request, @RequestBody UserDto userDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.updatePwd(userDto);
    }

    @PutMapping
    public ServerResponse updateUser(HttpServletRequest request, @RequestBody UserDto userDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        if (RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) || RoleEnum.AGENT.getRoleId().equals(userInfo.getRoleId())) {
            return userService.updateUser(userDto);
        } else {
            return ServerResponse.createByErrorMessage("您没有权限操作该方法");
        }
    }

    @GetMapping("/findById/{id}")
    public ServerResponse findById(@PathVariable int id) {
        return ServerResponse.createBySuccess(userService.findUserById(id));
    }

    @PutMapping("/updateWelfareReturnCoinSum")
    public ServerResponse updateWelfareReturnCoinSum(@RequestBody UserDto userDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.updateWelfareReturnCoinSum(userDto);
    }

    /**
     * 新增员工
     *
     * @param request
     * @param userDto
     * @return
     */
    @PostMapping("/addUserAndAddRole")
    public ServerResponse addUserAndAddRole(HttpServletRequest request, @RequestBody UserDto userDto) {
        try {
            UserInfo userInfo = userService.getLoginUserInfo(request);
            userDto.setUserInfo(userInfo);
            userService.addUserAndAddRole(userDto);
            return ServerResponse.createBySuccess();
        } catch (Exception e) {
            return ServerResponse.createByErrorMessage(e.getMessage());
        }
    }

    @PutMapping("/updateInfo")
    public ServerResponse updateInfo(HttpServletRequest request, @RequestBody UserDto userDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.updateInfo(userDto);
    }

    @PutMapping("/resetPwdById")
    public ServerResponse resetPwdById(@RequestBody UserDto userDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.resetPwdById(userDto);
    }

    @PostMapping("/getCode")
    public ServerResponse getCode(@RequestBody UserDto userDto) {
        return userService.getCode(userDto);
    }

    @PostMapping("/forgetPassword")
    public ServerResponse forgetPassword(@RequestBody UserDto userDto){
        return userService.forgetPassword(userDto);
    }

    @GetMapping("/changeSignStatus")
    public ServerResponse changeSignStatus(HttpServletRequest request) {
        UserDto userDto = new UserDto();
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setId(userInfo.getId());
        return userService.changeSignStatus(userDto);
    }
    @PutMapping("/resetOperatePwdById")
    public ServerResponse resetOperatePwdById(@RequestBody UserDto userDto, HttpServletRequest request) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        userDto.setUserInfo(userInfo);
        return userService.resetOperatePwdById(userDto);
    }
}
